﻿package com.dusk.game
{
	import com.dusk.net.NativeFile;
	import com.dusk.util.SystemUtil;
	import com.dusk.util.TimeUtil;
	
	import flash.utils.getQualifiedClassName;
	
	public class Logger
	{
		
		public static function log(content:String):void
		{
			if (SystemUtil.isDebugger())
			{
				trace(content);
			}
			if(Debug.getIns())
			{
				Debug.getIns().logInfo(content,1);
			}
		}
		
		public static function logToFile(content:String):void
		{
			NativeFile.append(content + "\n", NativeFile.getAppFolderFileUrl("log.log"));
		}
		
		/**
		 * 记录当前时间
		 */
		public static function logTime():void
		{
			logToFile(TimeUtil.getCurrentDate() + " " + TimeUtil.getCurrentTime());
		}
		
		/**
		 * 记录错误日志
		 * @param msg 错误信息，类型为String或者Error，如果是Error，则会将其堆栈信息记录到日志中
		 * @param tarClass 错误发生的类，类型为String或者Class，如果是Class，则会将其类名记录到日志中
		 */
		public static function logError(msg:*, tarClass:* = null):void
		{
			if (msg is String)
			{
				log(msg);
				logToFile(msg);
			}
			else if (msg is Error)
			{
				log((msg as Error).getStackTrace());
				logToFile((msg as Error).getStackTrace());
			}
			if(tarClass != null)
			{
				if (tarClass is String)
				{
					log("Class: " + tarClass);
					logToFile("Class: " + tarClass);
				}
				else
				{
					log("Class: " + getQualifiedClassName(tarClass).toString());
					logToFile("Class: " + getQualifiedClassName(tarClass).toString());
				}
			}
		}
	}
}